all files / backgrid-text-cell/ backgrid-text-cell.js

86.49% Statements 32/37
74.07% Branches 20/27
87.5% Functions 7/8
88.89% Lines 32/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179                                                                                                                                                                                                                                                                                                     
/*
  backgrid-text-cell
  http://github.com/wyuenho/backgrid
 
  Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
  Licensed under the MIT @license.
*/
(function (root, factory) {
 
  Iif (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['underscore', 'backgrid'], factory);
  } else Iif (typeof exports === 'object') {
    // CommonJS
    module.exports = factory(require("underscore"),
                             require("backgrid"));
  } else {
    // Browser globals
    factory(root._, root.Backgrid);
  }
 
}(this, function (_, Backgrid)  {
 
  var exports = {};
 
  /**
     Renders a form with a text area and a save button in a modal dialog.
 
     @class Backgrid.Extension.TextareaEditor
     @extends Backgrid.CellEditor
  */
  var TextareaEditor = exports.TextareaEditor = Backgrid.Extension.TextareaEditor = Backgrid.CellEditor.extend({
 
    /** @property */
    tagName: "div",
 
    /** @property */
    className: "modal fade",
 
    /** @property {function(Object, ?Object=): string} template */
    template: function (data) {
      return '<div class="modal-dialog"><div class="modal-content"><form><div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button><h3>' + data.column.get("label")  + '</h3></div><div class="modal-body"><textarea cols="' + data.cols + '" rows="' + data.rows + '">' + data.content + '</textarea></div><div class="modal-footer"><input class="btn btn-primary" type="submit" value="Save"/></div></form></div></div>';
    },
 
    /** @property */
    cols: 80,
 
    /** @property */
    rows: 10,
 
    /** @property */
    events: {
      "keydown textarea": "clearError",
      "submit": "saveOrCancel",
      "hide.bs.modal": "saveOrCancel",
      "hidden.bs.modal": "close",
      "shown.bs.modal": "focus"
    },
 
    /**
       @property {Object} modalOptions The options passed to Bootstrap's modal
       plugin.
    */
    modalOptions: {
      backdrop: false
    },
 
    /**
       Renders a modal form dialog with a textarea, submit button and a close button.
    */
    render: function () {
      this.$el.html($(this.template({
        column: this.column,
        cols: this.cols,
        rows: this.rows,
        content: this.formatter.fromRaw(this.model.get(this.column.get("name")))
      })));
 
      this.delegateEvents();
 
      this.$el.modal(this.modalOptions);
 
      return this;
    },
 
    /**
       Event handler. Saves the text in the text area to the model when
       submitting. When cancelling, if the text area is dirty, a confirmation
       dialog will pop up. If the user clicks confirm, the text will be saved to
       the model.
 
       Triggers a Backbone `backgrid:error` event from the model along with the
       model, column and the existing value as the parameters if the value
       cannot be converted.
 
       @param {Event} e
    */
    saveOrCancel: function (e) {
      if (e && e.type == "submit") {
        e.preventDefault();
        e.stopPropagation();
      }
 
      var model = this.model;
      var column = this.column;
      var val = this.$el.find("textarea").val();
      var newValue = this.formatter.toRaw(val);
 
      if (_.isUndefined(newValue)) {
        model.trigger("backgrid:error", model, column, val);
 
        Eif (e) {
          e.preventDefault();
          e.stopPropagation();
        }
      }
      else if (!e || e.type == "submit" ||
               (e.type == "hide" &&
                newValue !== (this.model.get(this.column.get("name")) || '').replace(/\r/g, '') &&
                confirm("Would you like to save your changes?"))) {
 
        model.set(column.get("name"), newValue);
        this.$el.modal("hide");
      }
      else Iif (e.type != "hide") this.$el.modal("hide");
    },
 
    /**
       Clears the error class on the parent cell.
     */
    clearError: _.debounce(function () {
      if (!_.isUndefined(this.formatter.toRaw(this.$el.find("textarea").val()))) {
        this.$el.parent().removeClass("error");
      }
    }, 150),
 
    /**
       Triggers a `backgrid:edited` event along with the cell editor as the
       parameter after the modal is hidden.
 
       @param {Event} e
    */
    close: function (e) {
      var model = this.model;
      model.trigger("backgrid:edited", model, this.column,
                    new Backgrid.Command(e));
    },
 
    /**
       Focuses the textarea when the modal is shown.
    */
    focus: function () {
      this.$el.find("textarea").focus();
    }
 
  });
 
  /**
     TextCell is a string cell type that renders a form with a text area in a
     modal dialog instead of an input box editor. It is best suited for entering
     a large body of text.
 
     @class Backgrid.Extension.TextCell
     @extends Backgrid.StringCell
  */
  exports.TextCell = Backgrid.Extension.TextCell = Backgrid.StringCell.extend({
 
    /** @property */
    className: "text-cell",
 
    /** @property  */
    editor: TextareaEditor
 
  });
 
  return exports;
 
}));